package com.slunk.platforms;

import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

import com.slunk.platforms.SRenderableNode;

public class SSpriteCollection {
	Array<SRenderableNode> children;
	
	public SSpriteCollection() {
		children = new Array<SRenderableNode>();
	}
	
	
	public void addChild(SRenderableNode sprite, int zIndex) {
		/*
		if (children.size == 0) {
			sprite.z_order = 0;
		} else {
			SRenderableNode lastSprite = children.get(children.size - 1);
			sprite.z_order = lastSprite.z_order + 1;
		}
		*/
		sprite.z_order = zIndex;
		children.add(sprite);
		children.sort();
	}
	
	public void addChild(SRenderableNode sprite) {
		
		int z_order = 0;
		if (children.size == 0) {
			//sprite.z_order = 0;
			//keep same
		} else {
			SRenderableNode lastSprite = children.get(children.size - 1);
			z_order = lastSprite.z_order + 1;
		}
		addChild(sprite,z_order);
		//children.add(sprite);
	}
	
	public void removeChild(SRenderableNode sprite) {
		sprite.onExit();
		children.removeValue(sprite,true);
	}
	
	public void drawAll(SpriteBatch batch) {
		for (SRenderableNode sprite : children) {
			System.out.println(sprite.alias);
			sprite.drawObject(batch);
			
			//just run onEnter once. Set the flag to true
			if (!sprite.onEnterFlag) {
				sprite.onEnter();
				sprite.onEnterFlag = true;
			}
		}
	}
}
